<template>
  <div class="AlarmConfigBody">
    <el-row :gutter="20">
      <el-col :span="6">
        <span class='alarmConfigTitle'>{{$t('是否启用监控告警')}}</span>
        <div class='alarmConfigSwitch'>
          <el-switch :width="lang === 'zh'?60:70" v-model="status" class="status switch" @change="statusChange(status)" active-color="#009966" inactive-color="#CCCCCC" active-value="on" inactive-value="off" :active-text="$t('启用')" :inactive-text="$t('停用')">
          </el-switch>
        </div>
      </el-col>
    </el-row>
    <el-divider content-position="left">{{$t('smtp服务器配置')}}</el-divider>
    <el-table size='mini' ref="multipleTable" border :data="configList" tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="host" :label="$t('服务器IP')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.host" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="port" :label="$t('服务器端口')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.port" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="encryption" :label="$t('加密方式')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="encryptionArray[scope.row.encryption]" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="sender-email" :label="$t('邮件发送者')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row['sender-email']" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="auth-method" :label="$t('验证方式')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="authMethodArray[scope.row['auth-method']]" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="user" :label="$t('用户名')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.user" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="100">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' + (scope.$index + 1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination> -->

    <config-dialog v-if="configDialogVisible" ref="refConfigDialogVisible"></config-dialog>
  </div>
</template>
<script>
import { switchAlert, getSmtpInfo, getAlertStatus } from '@/api/system/alarm/config'
import ConfigDialog from './components/ConfigDialog'
// import 'view-design/dist/styles/iview.css'
export default {
  name: 'AlarmConfig',
  components: {
    ConfigDialog
  },
  data () {
    return {
      configDialogVisible: false,
      lang: this.$i18n.locale,
      configList: [],
      status: 'off',
      encryptionArray: {
        'plain': this.$t('明文'),
        'ssl': 'SSL',
        'tls': 'TLS'
      },
      authMethodArray: {
        'none': this.$t('无'),
        'login': this.$t('登录')
      }
    }
  },
  async created () {
    await this.listData()
    await getAlertStatus().then(res => {
      if (res.status === 0) {
        this.status = res.info[0].status
      }
    })
  },
  methods: {
    // 列表
    async listData () {
      await getSmtpInfo().then(res => {
        if (res.status === 0) {
          this.configList = res.info
        }
      })
    },
    // 编辑按钮表单
    dialogVisible (data) {
      this.configDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refConfigDialogVisible.init(data)
      })
    },
    statusChange (data) {
      switchAlert({ status: data }).then(res => {
        if (res.status === 0) {
          this.$message({
            message: this.$t(res.msg),
            type: 'success'
          })
        }
      })
    }
  }
}
</script>
<style lang="less">
.AlarmConfigBody {
  margin-top: 60px !important;

  .el-divider__text {
    font-size: 20px;
  }

  .el-divider {
    margin: 40px 0 20px;
  }
}
</style>
<style lang="less" scoped>
.AlarmConfigBody {
  margin-top: 15px;
  // margin-left: 30px;
  line-height: 40px;

  .el-col {
    width: 300px;
    margin-left: 20px;
    margin-right: 3px;
    margin-bottom: 3px;
    background-color: #eeeeee;
  }

  .el-row {
    max-width: 1250px;
  }
}

.alarmConfigTitle {
  width: 185px;
  margin-left: 10px;
  display: inline-block;
}
.alarmConfigSwitch {
  width: 80px;
  text-align: right;
  display: inline-block;
}
</style>
